//
//  HMSlider.m
//  Pods
//
//  Created by holin on 4/1/15.
//
//

#import "HMSlider.h"

@interface HMSlider() <UIScrollViewDelegate>

@property (strong, nonatomic) NSArray *images;
@property (strong, nonatomic) UIPageControl *pageControl;
@property NSInteger currentIndex;

@end

@implementation HMSlider

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initialization];
    }
    
    return self;
}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initialization];
    }
    return self;
}


- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initialization];
    }
    
    return self;
}


- (void)initialization
{
//    self.layer.cornerRadius = 20;
    self.pagingEnabled = YES;
    self.showsVerticalScrollIndicator = NO;
    self.showsHorizontalScrollIndicator = NO;
    self.bounces = NO;
    self.delegate = self;
    
    UITapGestureRecognizer *viewTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTaped:)];
    [self addGestureRecognizer:viewTapRecognizer];
}


- (void)initWithImages:(NSArray *)images {
    self.images = images;
    
    
    NSLog(@"before My view frame: %@", NSStringFromCGRect(self.frame));
    [self setupPageControl];

    // make real frame
    [self setNeedsDisplay];
    [self layoutIfNeeded];
    
    NSLog(@"after My view frame: %@", NSStringFromCGRect(self.frame));

    
    [self setupSlider];
    
    [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(autoScroll) userInfo:nil repeats:YES];
    
}

#pragma mark - ScrollView delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    _currentIndex = scrollView.contentOffset.x / CGRectGetWidth(scrollView.frame);
    _pageControl.currentPage = _currentIndex;
}

# pragma mark - Helper
- (void)setupSlider {
    
    [self.images enumerateObjectsUsingBlock:^(UIImage *image, NSUInteger idx, BOOL *stop) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.frame) * idx, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.image = image;
        [self addSubview:imageView];
    }];
    
    self.contentSize = CGSizeMake(CGRectGetWidth(self.frame) * self.images.count, CGRectGetHeight(self.frame));

}

- (void)setupPageControl {
    _pageControl = [UIPageControl new];

    float h = 20.0;
    float y = self.frame.origin.y + self.frame.size.height - h;
    CGRect frame = CGRectMake(self.frame.origin.x, y, self.frame.size.width, h);
    _pageControl.frame = frame;
    [self.superview addSubview:_pageControl];
    _pageControl.numberOfPages = self.images.count;
}

# pragma mark - Selector
- (void)autoScroll {
    float w = self.contentOffset.x;
    w += self.frame.size.width;
    if (w >= self.contentSize.width) {
        w = 0;
    }
    
    [self setContentOffset:CGPointMake(w, 0) animated:YES];
}

- (void)viewTaped:(UITapGestureRecognizer*)sender {
    if (self.sliderDelegate &&
        [self.sliderDelegate respondsToSelector:@selector(tapedAtIndex:)]) {
        [self.sliderDelegate tapedAtIndex:_currentIndex];
    }
}


@end
